﻿Imports System.Data.OleDb

Public Class UserProfile


    Inherits System.Web.UI.Page
    Public Shared UsernameCarry1 As String
    Public Shared UsernameCarry2 As String
    Public Shared UsernameCarry3 As String


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If System.Web.HttpContext.Current.User.Identity.IsAuthenticated Then


        Else
            Response.Redirect("Account/Login.aspx")
        End If

        If Not IsPostBack Then

            Dim sql As String = "Select * FROM UserProfile WHERE Username=@19"


            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            conn.Open()

            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

            cmd.Parameters.AddWithValue("@f19", User.Identity.Name)

            Dim datareader = cmd.ExecuteReader()


            datareader.Read()

            If Not IsDBNull(datareader("Username")) Then UsernameCarry1 = datareader("Username") 'this statement allows the username to be stored in a variable which is then used to combine and format with a URL to bring up a record associated with that username (person who is currently logged in).
            If Not IsDBNull(datareader("Username")) Then UsernameCarry2 = datareader("Username")
            If Not IsDBNull(datareader("Username")) Then UsernameCarry3 = datareader("Username")

            If Not IsDBNull(datareader("FirstName")) Then lblfirstname.Text = datareader("FirstName")
            If Not IsDBNull(datareader("LastName")) Then lbllastname.Text = datareader("LastName")
            If Not IsDBNull(datareader("DOB")) Then lbldob.Text = (datareader("DOB"))
            If Not IsDBNull(datareader("UserImage")) Then imgprofilepicture.ImageUrl = datareader("UserImage")


            If Not IsDBNull(datareader("Address1")) Then lbladdress1.Text = datareader("Address1")
            If Not IsDBNull(datareader("Address2")) Then lbladdress2.Text = datareader("Address2")
            If Not IsDBNull(datareader("Town")) Then lbltown.Text = datareader("Town")
            If Not IsDBNull(datareader("City")) Then lblcity.Text = datareader("City")
            If Not IsDBNull(datareader("City")) Then lblcity2.Text = datareader("City")
            If Not IsDBNull(datareader("Postcode")) Then lblpostcode.Text = datareader("Postcode")
            If Not IsDBNull(datareader("TelNo")) Then lbltelno.Text = datareader("TelNo")
            If Not IsDBNull(datareader("AboutMe")) Then txtaboutme.Text = datareader("AboutMe")
            If Not IsDBNull(datareader("X6CarsDriven")) Then txtexperience.Text = datareader("X6CarsDriven")
            If Not IsDBNull(datareader("FavouriteColour")) Then lblfavouritecolour.Text = datareader("FavouriteColour")
            If Not IsDBNull(datareader("MaxSpeedDriven")) Then lblmaxspeeddriven.Text = datareader("MaxSpeedDriven")
            If Not IsDBNull(datareader("PreferedFuel")) Then lblpreferedfuel.Text = datareader("PreferedFuel")
            If Not IsDBNull(datareader("PreferedLitre")) Then lblpreferedlitre.Text = datareader("PreferedLitre")
            If Not IsDBNull(datareader("Gender")) Then lblgender.Text = datareader("Gender")
            If Not IsDBNull(datareader("RegistrationDate")) Then lblregistrationdate.Text = datareader("RegistrationDate")


            datareader.Close()


            'the sql statement below displays the number of discussions created by the current user currently logged into the system. It counts up all the discussions ID and groups them by the foreign key UserIDFK. Thos line of code Membership.GetUser(User.Identity.Name).ProviderUserKey outputs the data by idenfiying the user currently logged in by NAME using a USERKEY. 
            Dim sql2 As String = "SELECT * FROM DiscussionAddedCount WHERE UserName=@50"



            Dim conn2 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            conn2.Open()

            Dim cmd2 As OleDbCommand = New OleDbCommand(sql2, conn2)

            cmd2.Parameters.AddWithValue("@f50", User.Identity.Name)

            Dim datareader2 = cmd2.ExecuteReader()


            datareader2.Read()

            'this statement outputs the total number of discussions added by a particular to a label with an ID = lbldiscussionsadded in a text format. 
            If Not IsDBNull(datareader2("CountOfDiscussionID")) Then lbldiscussionsadded.Text = (datareader2("CountOfDiscussionID"))


            Dim sql3 As String = "SELECT * FROM PostsQueryCount WHERE UserName=@f100"

            Dim conn3 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            conn3.Open()

            Dim cmd3 As OleDbCommand = New OleDbCommand(sql3, conn3)

            cmd3.Parameters.AddWithValue("@f100", User.Identity.Name)

            Dim datareader3 = cmd3.ExecuteReader()


            datareader3.Read()

            If Not IsDBNull(datareader3("CountOfPostID")) Then lblpostsAdded.Text = (datareader3("CountOfPostID"))

        End If






    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Response.Redirect("UserProfileEdit.aspx")

    End Sub

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Response.Redirect("galleryAlbums.aspx?UserID=" & User.Identity.Name)
    End Sub

    Protected Sub btnuserPostsAdded_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnuserPostsAdded.Click
        Response.Redirect("userPostsAdded.aspx?UserID=" & User.Identity.Name)
    End Sub

    Protected Sub btnuserDiscussionsAdded_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnuserDiscussionsAdded.Click
        Response.Redirect("userDiscussionsAdded.aspx?UserID=" & User.Identity.Name)
    End Sub
End Class